<head>
<!-- Copyright (C) 2020 mn

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. 
--> 
<title>quasi 1.0</title>
<style>
div {overflow: scroll; font-family: monospace; font-size: 19px; font-weight: bold;}
textarea {overflow: scroll; font-family: monospace; font-size: 19px; font-weight: bold;}
</style>
</head>

<body>
<span id="progrun">
</span>
<div id="dtop" style="position: absolute; top: 1%; left: 1%; width: 98%; height: 35%;"><br>&nbsp;display window</div>
<textarea id="dleft" style="position: absolute; top: 38%; left: 1%; width: 32%; height: 58%;">

# import window 

m = get -1 ; str ; left 1
p ; for 1 5 1
    hello = date ; mid 2 1
    now = get "u tc" ; split now " "  
    now = join now "" ; prints 
    m ; prints
    now = get hello ; print
    next
now ; title</textarea>
<div id="dmid" style="position: absolute; top: 38%; left: 35%; width: 64%; height: 58%;"
onclick="javascript:runimport();"><br>&nbsp;program editor (not implemented yet)<br><br>&nbsp;click here to run program in import/export window on left<br><br></div>

<script>dtop.style.background = 'white'; dtop.style.bordercolor = 'black'; dtop.style.border = 'solid';

dleft.style.background = 'white'; dleft.style.bordercolor = 'black'; dleft.style.border = 'solid';
// dleft.innerHTML = "<textarea style='width: 100%; height: 100%;'></textarea>";

dmid.style.background = 'white'; dmid.style.bordercolor = 'black'; dmid.style.border = 'solid';

function outd(p) {
    dtop.innerHTML += "<span style='color: orange;'>" + p + "</span><br>\n";
    }

function left(s, l) {
    return s.substring(0, l);
    }

function right(s, l) {
    return s.substring(s.length-l, s.length);
    }

var addfuncs = "\n\
function q_cls(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    dtop.innerHTML = \"\";\n\
    }\n\
\n\
function q_date(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    d = Date().toLocaleString().split(\" \");\n\
    months = {\n\
        'Jan': '01',\n\
        'Feb': '02',\n\
        'Mar': '03',\n\
        'Apr': '04',\n\
        'May': '05',\n\
        'Jun': '06',\n\
        'Jul': '07',\n\
        'Aug': '08',\n\
        'Sep': '09',\n\
        'Oct': '10',\n\
        'Nov': '11',\n\
        'Dec': '12',\n\
        }\n\
    return (months[d[1]] + \"/\" + d[2] + \"/\" + d[3] + \"_\" + d[4] + \"_\" + d[0]).split(\"_\");\n\
    }\n\
\n\
function q_end(p) {\n\
    progrun.innerHTML = \"<!-- -->\";\n\
    }\n\
\n\
function q_print(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    dtop.innerHTML = dtop.innerHTML + p + \"<br>\\n\";\n\
    }\n\
\n\
function q_prints(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    dtop.innerHTML = dtop.innerHTML + p;\n\
    }\n\
\n\
function q_str(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    return \"\" + p;\n\
    }\n\
\n\
function q_title(p) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    document.title = p;\n\
    }\n\
\n\
function q_get(p, s) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    return (s);\n\
    }\n\
\n\
function q_left(p, s) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    if (typeof(p) == \"object\") {\n\
        var t = [];\n\
        for(count = 0; count < s; count++) {\n\
            t.push(p[count]);\n\
            }\n\
        }\n\
    else {\n\
        var t = p.substring(0, s);\n\
        }\n\
    return t;\n\
    }\n\
\n\
function q_right(p, s) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    if (typeof(p) == \"object\") {\n\
        var t = [];\n\
        for(count = p.length - s; count < p.length; count++) {\n\
            t.push(p[count]);\n\
            }\n\
        }\n\
    else {\n\
        var t = p.substring(p.length - s, p.length);\n\
        }\n\
    return t;\n\
    }\n\
\n\
function q_split(p, s, o) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    return s.split(o);\n\
    }\n\
\n\
function q_join(p, s, o) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    return s.join(o);\n\
    }\n\
\n\
function q_mid(p, s, o) {\n\
    if (progrun.innerHTML == \"<!-- -->\") { return 0; }\n\
    if (typeof(p) == \"object\") {\n\
        var t = [];\n\
        for(count = s - 1; count < s -1 + o; count++) {\n\
            t.push(p[count]);\n\
            }\n\
        }\n\
    else {\n\
        var t = p.substring(s - 1, s - 1 + o);\n\
        }\n\
    return t;\n\
    }\n\
\n\
";

eval(addfuncs); q_cls(0);

cmds = {
    cls: 0,
    date: 0,
    end: 0,
    for: -4,
    get: 1,
    join: 2,
    left: 1,
    mid: 2,
    print: 0,
    prints: 0,
    right: 1,
    split: 2,
    str: 0,
    title: 0
    }

function runimport() { 
    parsed = textparse(); 
    if (parsed != "") { 
        dtop.innerHTML = "";
        eval(parsed);
        dtop.innerHTML += "<br>" + parsed.split("\n").join("<br>\n");
        }
    }

function textparse() { 
    vars = { }
    //q_cls();
    progrun.innerHTML = "";
    var p = "";
var cmdcount = 0;
    var petc = "";
    var getcmd = "";
    var getvar = "";
    var cc = dleft.value + "\n";
    var codeout = "";
    var bu = 0;
    var inquotes = 0;
    var commented = 0;
    var tokcount = 0;
    var mainvar = "";
    var mainvarplus = "";
    var prevcmd = "";
    var tokisnum = 0;
    var special = "";
    codeout += "vars = { }\n" + addfuncs;
    for(count = 0; count < cc.length; count++) {
        p = cc.substring(count, count + 1);
        if (commented == 0 && inquotes == 0) {
            if (p == "=" || p == ":" || p == ";") {
                p = " "; // allow optional ; or =
                }
            }
        if (p == "#") { 
            commented = 1; 
            }
        if (p == "\"") { 
            if (inquotes == 1) {
                inquotes = 0;
                } 
            else {
                inquotes = 1;
                }
            }
        if (commented == 0 && inquotes == 0 && left(petc, 1) != "#") {
            if (p == " " || p == "\n") {
                if (petc.trim() != "") { tokcount += 1 }
                if (p == " ") { 
                    p = ""; 
                    }
                if (petc != "") {
                    tokisnum = 0;
                    getcmd = undefined;
                    if ("-01234567890".indexOf(petc[0]) > -1 || petc[0] == "\"") { 
                        tokisnum = 1; getcmd = undefined;
                        }
                    else {
                        eval("getcmd=cmds." + petc + ";");
                        }

                    if (getcmd != undefined) {
                        cmdcount = getcmd;
                        mainvarplus = "";
                        getcmd = "q_" + petc + "(";
                        prevcmd = getcmd;
                        if (tokcount > 1) { 
                            if (cmdcount == 0) {
                                codeout += "bu = " + mainvar + "; " + mainvar + " = " + getcmd + mainvar + "); "; 
                                codeout += "if (" + mainvar + " == undefined) { " + mainvar + " = bu; }\n"; 
                                }
                            if (petc == "for") { special = "for"; cmdcount = -4}
                            }
                        else {
                            codeout += getcmd + "); "; 
                            }
                        }
                    else {
                        if (petc != "")  {
                        try {
                            getvar = 0 ; 
                            if (tokisnum == 0) { eval("getvar=vars." + petc + ";"); }
                            if (tokcount == 1) { 
                                if (petc == "prev" || petc == "next") {
                                    if (petc == "next") {
                                         codeout += "}\n"; 
                                        } 
                                    } 
                                else {
                                     mainvar = "vars." + petc;
                                     codeout += "if (vars." + petc + " == undefined) { vars." + petc + " = 0; }\n"; 
                                     }
                                }
                            else {
                                if (cmdcount == -2 && special == "for") {
                                    codeout += mainvar + "+= " + petc + ") {\n" 
                                    cmdcount += 1; special = "";
                                    }
                                if (cmdcount == -3 && special == "for") {
                                    codeout += mainvar + " < (" + petc + " + 1); "; 
                                    cmdcount += 1;
                                    }
                                if (cmdcount == -4 && special == "for") {
                                    codeout += "for(" + mainvar + " = " + petc + "; "; 
                                    cmdcount += 1; 
                                    }
                                if (cmdcount == 0) {
                                    codeout += "bu = " + mainvar + "; " + mainvar + " = " + getcmd + mainvar + mainvarplus + "); "; 
                                    codeout += "if (" + mainvar + " == undefined) { " + mainvar + " = bu; }\n"; 
                                    }
                                if (cmdcount == 1) {
                                    if (tokisnum == 0) { 
                                        mainvarplus += ", " + "vars." + petc; cmdcount -= 1; }
                                    else {
                                        mainvarplus += ", " + petc; cmdcount -= 1;
                                        }

                                    codeout += "bu = " + mainvar + "; " + mainvar + " = " + prevcmd + mainvar + mainvarplus + "); ";
                                    codeout += "if (" + mainvar + " == undefined) { " + mainvar + " = bu; }\n"; 

                                    }
                                if (cmdcount > 1) {
                                    if (tokisnum == 0) { 
                                        mainvarplus += ", " + "vars." + petc; cmdcount -= 1; }
                                    else {
                                        mainvarplus += ", " + petc; cmdcount -= 1;
                                        }
                                    }
                                }
                            }
                        catch(err) { }
                            }
                        if (getvar == undefined) { eval("vars." + petc + " = 0;"); }
                        }
                    petc = "";
                    }	
                }
            }
        if (p == "\n") {
            tokcount = 0; 
            commented = 0;
            inquotes = 0;
            if (left(petc, 1) == "#") {
                codeout += "// " + petc + "\n\n"; 
                }
            else {
                codeout += "\n";
                }
            petc = ""; }
        else {
            if (inquotes == 0 && commented == 0) {
                petc = (petc + p).trim(); }
            else {
                petc = petc + p;
                }
            }
        }
    return codeout;
    }

dtop.innerHTML = "<br>" + document.title + "<br>Copyright (C) 2020 mn<br><br>This program is free software: you can redistribute it and/or modify it under the terms of the <a href='gpl-3.0.txt'>GNU General Public License</a> as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.<br><br>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.<br><br>You should have received a copy of the GNU General Public License along with this program. If not, see <a href='https://www.gnu.org/licenses/'>https://www.gnu.org/licenses/</a>.";

</script>
</body>
